var express = require('express');
const { uploadFile } = require("../utils/upload.js");
const { get, update, remove, create, getById,getall} = require("../servies/supplierFoodsService.js");
var router = express.Router();

//上传图片
router.post('/upload', async function (req, res, next) {
    const { data, success } = await uploadFile(req, {
        fileType: "supplierFoods",//文件包含目录
        path: "./public/imgs"//图片存放路径，自定义

    });
    if (success) {
        const { _id, url } = data;
            if (_id) {
                await supplierFoodsService.update(_id, {
                    $push: JSON.stringify({
                        imgs: url
                    })
                });
                const result = await supplierFoodsService.getById(_id);
                res.send(result);
            }else{
                res.send(url)
            }
    }
});
//分页
router.get('/', async function (req, res, next) {
    const { curPage, eachPage, type, name,sort } = req.query;
    if (type != "" && name != "") {
        let nameData = {}
        nameData[type] = name
        const data = await get({ curPage, eachPage, nameData,sort })
        res.send(data)
    } else {
        const data = await get({ curPage, eachPage,sort });
        res.send(data)
    }
});

//请求图片
router.get('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = await getById({ _id });
    res.send(data);
});
//更新
router.post("/update", async function (req, res, next) {
    const { _id, date,name,type,status,clazz,material,method,standard,exclusive,flavor,special,origin,warranty,feature,packing,price,imgs,count} = req.body;
    const data = await update({ _id }, { date,name,type,status,clazz,material,method,standard,exclusive,flavor,special,origin,warranty,feature,packing,price,imgs,count });
    res.send(data);
});


//新增
router.post("/create", async function (req, res, next) {
    let data= {date,name,type,status,clazz,material,method,standard,exclusive,flavor,special,origin,warranty,feature,packing,price,imgs,count} = req.body;
    const result = await create(data);
    res.send(result);
});

//删除
router.post("/deldate", async function (req, res, next) {
    const { _id } = req.body;
    const data = await remove({ _id });
    res.send(data);
});
router.patch('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const params = req.body;
    const data=await studentsService.update(_id, params);
    if(data.nModified&&params["$pull"]){
        const {imgs}=JSON.parse(params["$pull"]);
        studentsService.delImg(imgs);
    }
    const result = await studentsService.getById(_id, params);
    res.send(result);
});
//删除图片
router.patch("/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const params = req.body;
    for (let k in params) {
        if (/^\$/.test(k)) params[k] = JSON.parse(params[k]);
    }
    const data = await update(_id, params);
    res.send(data);
});


module.exports = router;